package cn.shiwei.user.controller;

import cn.shiwei.basic.exception.MyException;
import cn.shiwei.basic.util.LoginContextUtil;
import cn.shiwei.basic.util.PageList;
import cn.shiwei.basic.util.RequestStatus;
import cn.shiwei.user.domain.Logininfo;
import cn.shiwei.user.domain.User;
import cn.shiwei.user.domain.UserAddress;
import cn.shiwei.user.domain.UserDto;
import cn.shiwei.user.query.UserQuery;
import cn.shiwei.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/user")
@Api(tags = "用户接口",description = "用户接口详细描述")
public class UserController {

    @Autowired
    private IUserService userService;

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询部门",notes = "需要参数id")
    private User queryById(@PathVariable("id") Long id){
        return userService.findById(id);
    }

    @GetMapping
    @ApiOperation(value = "查询全部",notes = "无参数")
    private List<User> queryAll(){
        return userService.loadAll();
    }

    @PostMapping
    @ApiOperation(value = "部门列表分页查询",notes = "需要传参 为高级查询的条件")
    private PageList<User> queryPage(@RequestBody UserQuery query){
        return userService.queryDate(query);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除删除",notes = "需要参数id")
    private RequestStatus delete(@PathVariable("id") Long id){
        try {
            userService.remove(id);
            return RequestStatus.me();
        } catch (Exception e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage("删除失败");
        }
    }

    @PutMapping()
    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
    private  RequestStatus addAndUpdate(@RequestBody User user){
        try {
            if(user.getId() == null){
                userService.save(user);
            } else {
                userService.update(user);
            }
            return RequestStatus.me();
        } catch (Exception e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage("操作失败");
        }
    }

    @PatchMapping
    @ApiOperation(value = "根据id批量删除",notes = "需要参数id")
    private RequestStatus patchDelete(@RequestBody List<Long> ids){
        try {
            userService.patchRemove(ids);
            return RequestStatus.me();
        } catch (Exception e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage("删除失败");
        }
    }

    @PostMapping("/register/phone")
    private RequestStatus register(@RequestBody UserDto userDto){
        try {
            userService.register(userDto);
            return RequestStatus.me();
        } catch (MyException e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage(e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage(e.getMessage());
        }
    }

    @GetMapping("/getAddress")
    public RequestStatus getAddress(HttpServletRequest request){
        try {
            Long id = LoginContextUtil.getLoininfo(request).getId();
            List<UserAddress> adds = userService.loadAddressByLogininId(id);
            return RequestStatus.me().setResultObj(adds);
        } catch (MyException e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage("系统繁忙");
        }
    }

    @PostMapping("/saveAddress")
    public RequestStatus getAddress(@RequestBody UserAddress userAddress){
        try {
            userService.saveAddress(userAddress);
            return RequestStatus.me();
        } catch (MyException e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage(e .getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage("系统繁忙!, ");
        }
    }

}
